package ldh.im.fxgui.rpc.service;

import ldh.im.fxbase.dao.ImGroupDao;
import ldh.im.fxbase.dao.ImSessionDao;
import ldh.im.fxbase.data.event.EventBusFactory;
import ldh.im.fxbase.data.event.GroupNameChange;
import ldh.im.fxbase.data.event.SessionChange;
import ldh.im.fxbase.data.pojo.ImGroup;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.util.BeanFactory;
import ldh.im.fxbase.util.ImHotMessageSingleton;

import java.sql.SQLException;
import java.util.Date;

public class GroupChangeDataService {

    private ImGroupDao groupDao = BeanFactory.getInstance().getBean(ImGroupDao.class);
    private ImSessionDao sessionDao = BeanFactory.getInstance().getBean(ImSessionDao.class);

    // 修改用户名称
    public void editGroupName(String userId, String groupCode, String groupName) throws SQLException {
        ImGroup imGroup = groupDao.getByUserIdAndId(userId, groupCode);
        imGroup.setName(groupName);
        imGroup.setUpdateTime(new Date());
        groupDao.updateNameAndDescription(imGroup);

        ImSession session = sessionDao.getByGroupCodeAndUserId(groupCode, userId);
        session.setName(groupName);
        session.setUpdateTime(new Date());
        sessionDao.updateSession(session);

        EventBusFactory.getInstance().post(new GroupNameChange(imGroup.getCode(), imGroup.getName()));
        EventBusFactory.getInstance().post(new SessionChange(session.getId()));
    }

    // 修改用户名称
    public void editGroupDescription(String userId, String groupCode, String groupDescription) throws SQLException {
        ImGroup imGroup = groupDao.getByUserIdAndId(userId, groupCode);
        imGroup.setDescription(groupDescription);
        imGroup.setUpdateTime(new Date());
        groupDao.updateNameAndDescription(imGroup);

        ImSession session = sessionDao.getByGroupCodeAndUserId(groupCode, userId);
        EventBusFactory.getInstance().post(new SessionChange(session.getId()));
    }
}
